
import { reactive } from 'vue'
// import assign from '../utils/assign'

const requestTypeMap = {
  basic: 0,
  system: 1,
  writePad: 2,
  answerMachine: 3,
  activity: 4,
  annex: 5,
  ppt: 6,
  screenRecord: 7,
  ggb: 8,
  device: 9,
  paint: 10,
  studentWork: 14,
  studentLive: 11,
  studentScreenBroadcast: 12,
  studentProjection: 13,
  studentKeyMark: 15,
  weblog: 16,
  book: 17,
  linkMicrophone: 18
}

// const SUCCESS = 200
const NOT_FOUND = 404
const FAILED = 500

class PcClient {
  constructor (requestType, operaton) {
    this.name = operaton.name
    this.requestType = requestType
    this.operationType = operaton.operationType
    this.successHandler = operaton.successHandler
    this.errorHandler = operaton.errorHandler
    this.ignoreErr = typeof operaton.ignoreErr === 'undefined' ? true : operaton.ignoreErr
    this._params = JSON.parse(JSON.stringify(operaton.params))
    this._data = JSON.parse(JSON.stringify(operaton.data))
    this.params = operaton.params ? reactive(operaton.params) : reactive({})
    this.data = operaton.data ? reactive(operaton.data) : reactive({})
  }

  reset () {
    this.params = reactive(JSON.parse(JSON.stringify(this._params)))
    this.data = reactive(JSON.parse(JSON.stringify(this._data)))
    return this
  }

  /**
  * 功能: H5调用PC端功能
  * @param {String} params 请求参数
  */
  send (params = {}, isNeedTips = false) {
    Object.assign(this.params, params)
    if (window.pcClientJavaScriptObject && window.pcClientJavaScriptObject.callPcClient) {
      return window.pcClientJavaScriptObject.callPcClient({
        RequestType: this.requestType,
        OperationType: this.operationType,
        RequestBody: this.params
      }).then((data) => {
        if (this.successHandler) {
          data = this.successHandler(data)
        }

        return data
      }).catch(err => {
        if (err.code === NOT_FOUND) {
          window.$ui.msg({ type: 'error', message: `【${this.name}】功能在当前环境下不可用` })
        } else if (err.code === FAILED) {
          window.$ui.msg({ type: 'error', message: `【${this.name}】操作失败` })
        } else {
          // SDVCCE-21556 针对部分杀毒软件导致上传 下载文件操作被截取 失败提示
          let errMsg = ((this.requestType === 1 && this.operationType === 1) || (this.requestType === 1 && this.operationType === 2)) ? '系统文件缺失，建议关闭安全软件后重新安装' : `调用【${this.name}】时发生未知错误`
          window.$ui.msg({ type: 'error', message: errMsg })
        }

        if (this.errorHandler) {
          return this.errorHandler(err)
        } else {
          if (!this.ignoreErr) {
            return err
          }
        }
      })
    } else {
      if (isNeedTips) {
        window.$ui.msg({ type: 'error', message: `【${this.name}】功能只能在客户端环境使用` })
      }
    }
  }
}

class TwPc {
  constructor (config) {
    this._config = config
    this._pcClientMap = []

    for (const [requestType, request] of Object.entries(config.client)) {
      for (const [operationType, operaton] of Object.entries(request)) {
        if (!this[requestType]) {
          this[requestType] = {}
        }

        this._pcClientMap.push({ method: `this.$twpc.${requestType}.${operationType}`, name: operaton.name, operationType: operaton.operationType })

        this[requestType][operationType] = new PcClient(requestTypeMap[requestType], operaton)
      }
    }
  }
}

export default {
  install (app, config = {}) {
    const twpc = new TwPc(config)

    window.$twpc = twpc

    app.config.globalProperties.$twpc = twpc
  }
}
